package com.zhongxi.guoyuan.view;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.OffLineShop.UI.OffLineShopDetailActivtiy;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.TopSearchAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.HomeSearchResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.view.personalCenter.ClearWeedDetailActivity;
import com.zhongxi.guoyuan.view.rewardCoin.RewardCoinDetailActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HomeSearchActivity extends BaseActivity implements View.OnClickListener, TopSearchAdapter.OnItemClickListener {

    private RecyclerView mRecyclerView;
    private EditText etSearch;
    private ImageView ivSearch;


    private String content;
    private String mTrim;
    private List<HomeSearchResult.DataBean> mList=new ArrayList<>();

    private TopSearchAdapter adapter;
    private JSONObject mCateInfo;
    private JSONObject mStore;
    private LinearLayoutManager layoutManager;
    private int lastItem;
    private boolean isLoading;
    private HomeSearchResult mHomeSearchResult;


    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_home_search);
        initTitle();
        mRecyclerView = (RecyclerView) findViewById(R.id.recycleview);
        etSearch = (EditText) findViewById(R.id.et_search);
        ivSearch = (ImageView) findViewById(R.id.iv_search);
        tvTitle.setText("搜索");

        layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        adapter = new TopSearchAdapter(this);
        mRecyclerView.setAdapter(adapter);

    }

    @Override
    protected void initData() {
        super.initData();
    }

    private void getdata() {
        Map<String, String> map = new HashMap<>();
        map.put("content", content);
        String str = "&page=" + pageNum ;
        getServer(Url.TOP_SEARCH, map, str, false, null, "post");
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson=new Gson();
        mHomeSearchResult = gson.fromJson(response,HomeSearchResult.class);
        MyApplication.getInstance().setSpValue(mHomeSearchResult.getSafeobj());
        if (mHomeSearchResult != null) {
            if (pageNum==1){
                mList.clear();
            }

            if (mHomeSearchResult.getData()!=null&& mHomeSearchResult.getData().size()>0){
                mList.addAll(mHomeSearchResult.getData());

                try {
                    JSONObject mJSONObject=new JSONObject(response);
                    mCateInfo = mJSONObject.optJSONObject("cateInfo");
                    mStore = mJSONObject.optJSONObject("store");

                    adapter.setList(mList,mCateInfo,mStore);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            if (mHomeSearchResult.getData().size()<10&& mHomeSearchResult.getData().size()>0){
                adapter.setFooter_state(2);
            }

            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        ivSearch.setOnClickListener(this);

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastItem + 1 == adapter.getItemCount() && !isLoading
                        ) {
                    if (mHomeSearchResult.getData()!=null&&mHomeSearchResult.getData().size() > 1) {
                        //滑动到底部加载更多
                        content = etSearch.getText().toString().trim();
                        pageNum++;
                        if (content != null && content.length() > 0) {
                           getdata();
                        } else {
                           getdata();
                        }
                        isLoading = true;
                        adapter.setFooter_state(1);
                    } else {
                        adapter.setFooter_state(2);
                    }

                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastItem = layoutManager.findLastVisibleItemPosition();
            }
        });

        adapter.setItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                onBackPressed();
                break;
            case R.id.iv_search:
                content = etSearch.getText().toString().trim();
                if (!TextUtils.isEmpty(content)){
                    getdata();
                }
                break;
        }

    }

    @Override
    public void onClick(View view, int position) {
        HomeSearchResult.DataBean mDataBean = mHomeSearchResult.getData().get(position);
        String mType = mDataBean.getType();
        if (mType.equals("marketgoods")){
            Intent intent = new Intent(HomeSearchActivity.this, RewardCoinDetailActivity.class);
            intent.putExtra("gid", mDataBean.getId());
            startActivity(intent);
        }else if (mType.equals("exchangegoods")){
            Intent intent = new Intent(HomeSearchActivity.this, ClearWeedDetailActivity.class);
            intent.putExtra("id", mDataBean.getId());
            Log.d("flag", "onClick: "+mDataBean.getId());
            startActivity(intent);
        }else if (mType.equals("offlinemarketstore")){
            Intent intent = new Intent(HomeSearchActivity.this, OffLineShopDetailActivtiy.class);
            intent.putExtra("offlineshopid", mDataBean.getId());
            startActivity(intent);
        }

    }
}
